<template>
  <div class="form-wrapper">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item label="关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="商品名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable>
          <el-option label="上架" value="1" />
          <el-option label="下架" value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
        <el-button @click="resetQuery">
          <i-ep-refresh />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </div>

  <el-card shadow="never">
    <template #header>
      <div class="table-header">
        <div>
          <el-button type="success" @click="openDialog('user-form')"> <i-ep-plus />新增 </el-button>
          <el-button type="danger" @click="handleDelete()"><i-ep-delete />删除</el-button>
        </div>
        <div>
          <el-button @click="openDialog('user-import')"><i-ep-top />导入 </el-button>
          <el-button class="ml-5" @click="handleExport"><i-ep-download />导出</el-button>
        </div>
      </div>
    </template>

    <el-table :data="pageData">
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column key="id" label="编号" align="center" prop="id" />
      <el-table-column key="name" label="商品名称" align="center" width="140" prop="name" />
      <el-table-column label="规格" width="100" align="center" prop="model" />
      <el-table-column label="包装规格" width="120" align="center" prop="pack" />
      <el-table-column label="数量" width="120" align="center" prop="number" />
      <el-table-column label="件数" align="center" width="120" prop="quantity" />
      <el-table-column label="仓库" align="center" width="120" prop="warehouse" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="row">
          <el-tag :type="row.status == 1 ? 'success' : 'info'">{{
            row.status == 1 ? '启用' : '禁用'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template #default="row">
          <div class="btn-control">
            <el-button type="primary" link size="small" @click="openDialog('user-form', row.id)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button type="primary" link size="small" @click="handleDelete(row.id)"
              ><i-ep-delete />删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total > 0"
      v-model:total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />
  </el-card>
</template>

<script setup>
import { ref, reactive } from 'vue'
const total = ref(30)
const queryFormRef = ref() // 查询表单DOM
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10
})

/** 空函数 */
const resetQuery = () => {}
const openDialog = () => {}
const handleExport = () => {}

const removeIds = ref([]) // 删除用户ID集合 用于批量删除
const handleDelete = (id) => {
  const userIds = [id || removeIds.value].join(',')
  if (!userIds) {
    ElMessage.warning('请勾选删除项')
    return
  }

  ElMessageBox.confirm('确认删除用户?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function () {
    // deleteUsers(userIds).then(() => {
    //   ElMessage.success('删除成功')
    //   resetQuery()
    // })
  })
}

/** 查询 */
const handleQuery = () => {
  console.log('查询表格')
}

const pageData = [
  {
    id: '1',
    name: '竹香酒三年',
    model: '240ML',
    pack: '6瓶装',
    number: '240瓶',
    quantity: '40箱',
    warehouse: '齐安仓库',
    createTime: '2016-05-03'
  },
  {
    id: '2',
    name: '竹香酒五年',
    model: '480ML',
    pack: '6瓶装',
    number: '240瓶',
    quantity: '40箱',
    warehouse: '阳岐路',
    createTime: '2016-05-02'
  },
  {
    id: '3',
    name: '竹香酒一年',
    model: '240ML',
    pack: '6瓶装',
    number: '240瓶',
    quantity: '40箱',
    warehouse: '阳岐路',
    createTime: '2016-05-04'
  },
  {
    id: '4',
    name: '竹香酒十年',
    model: '240ML',
    pack: '6瓶装',
    number: '240瓶',
    quantity: '40箱',
    warehouse: '阳岐路',
    createTime: '2016-05-01'
  }
]
</script>
<style lang="scss" scoped>
.form-wrapper {
  margin: 0 1.8rem;
  .el-form {
    display: flex;
    align-items: center;
    height: 6rem;
    .el-form-item {
      margin-bottom: 0;
    }
  }
}
.el-card {
  border: 0;
  ::v-deep(.el-card__header) {
    margin: 0 1.8rem;
    .table-header {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
